﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Space_Cleaner.SpaceCleaner
{
    public class SC_HUD : SC_IScriptEventListener
    {
        private Vector2 TrashCountPos = new Vector2(20, 10);
        private Vector2 CollisionCountPos = new Vector2(20, 30);
        private Vector2 AvgColPos = new Vector2(20, 50);

        private float TotalTime = 0;

        public SpriteFont Font { get; set; }

        public int TrashCount { get; set; }
        public int CollisionCount { get; set; }

        private Texture2D textureHUD;
        private Texture2D textureBar;
        private Vector2 hudPosition = new Vector2(20.0f,20.0f);
        private float size = 0.7f;
        private static int MAX_BARCOUNT = 23;
        private static int BAR_WIDTH = 15;
        private int barCount;

        public float AvgCol
        {
            get
            {
                return CollisionCount / TotalTime;
            }
            set
            {
                TotalTime = value;
            }
        }

        public SC_HUD(Texture2D _textureHUD, Texture2D _textureBar)
        {
            this.textureHUD = _textureHUD;
            this.textureBar = _textureBar;
            TrashCount = 0;
            CollisionCount = 0;
        }

        public void DrawHUD(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(textureHUD, hudPosition, null, Color.White, 0.0f, Vector2.Zero, this.size, SpriteEffects.None, 0f);
            for (int i = 0; i < barCount; i++)
                spriteBatch.Draw(textureBar, hudPosition + (new Vector2(44, 21) * this.size) + new Vector2(i * BAR_WIDTH, 0), null, Color.White, 0.0f, Vector2.Zero, this.size, SpriteEffects.None, 0f);
        }

        public void DrawTrashCount(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(Font, "TrashCount: " + TrashCount.ToString(), TrashCountPos, Color.White);                  
        }

        public void DrawCollisionCount(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(Font, "CollisionCount: " + CollisionCount.ToString(), CollisionCountPos, Color.White);
        }

        public void DrawAvgCol(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(Font, "Average Collisions(per sekond): " + AvgCol.ToString(), AvgColPos, Color.White);
        }

        public void onUpdate(GameTime gameTime, TimeSpan remainingTime)
        {
            TimeSpan startTime = gameTime.TotalGameTime + remainingTime;

            double div = gameTime.TotalGameTime.TotalMilliseconds / startTime.TotalMilliseconds;

            barCount = (int)(MAX_BARCOUNT * div);

            barCount = MAX_BARCOUNT - barCount;
        }

        public void onRun()
        {
        }
    }
}
